// WebWorld.cpp: implementation of the CWebWorld class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WebWorld.h"
#include "Myfunction.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

const TCHAR szHeaders[] =
	_T("Accept: text/*\r\nUser-Agent: FstInet\r\n");
const DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
const DWORD dwHttpRequestFlags =
	INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD;
/////////////////////////////////////////////////////////////////////////////
// CWebException -- used if something goes wrong for us

// will throw its own exception type to handle problems it might
// encounter while fulfilling the user's request.

IMPLEMENT_DYNCREATE(CWebException, CException)

CWebException::CWebException(int nCode)
	: m_nErrorCode(nCode)
{
}

void ThrowWebException(int nCode)
{
	CWebException* pEx = new CWebException(nCode);
	throw pEx;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWebWorld::CWebWorld()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
}

CWebWorld::~CWebWorld()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
}

/*
	Return 0 if success. Otherwise return error code.
*/
int CWebWorld::GetWebPage(LPCTSTR pszURL, CString &strPage)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	int nRetCode = 0;

	CInternetSession session(_T("FstInet"));
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	try
	{
		// check to see if this is a reasonable URL
		CString strServerName;
		CString strObject;
		INTERNET_PORT nPort;
		DWORD dwServiceType;

		if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
			dwServiceType != INTERNET_SERVICE_HTTP)
		{
			ThrowWebException(1);
		}

		pServer = session.GetHttpConnection(strServerName, nPort);

		pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
			strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
		pFile->AddRequestHeaders(szHeaders);
		pFile->SendRequest();

		DWORD dwRet;
		pFile->QueryInfoStatusCode(dwRet);

		// Here, dwRet, in other words, dwStatusCode
		//		200		URL located, transmission follows
		//		404		Requested URL not found
		//		401		The requested resource requires user authentication
		//		401 is HTTP_STATUS_DENIED

		// if access was denied, prompt the user for the password

		if (dwRet == HTTP_STATUS_DENIED)
		{
		//	DWORD dwPrompt;
		//	dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
		//		FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);

			// if the user cancelled the dialog, bail out

		//	if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
		//	{
				// Access denied: Invalid password
		//		ThrowWebException(1);
		//	}

			pFile->SendRequest();
			pFile->QueryInfoStatusCode(dwRet);
		}

		strPage.Empty();
		TCHAR szBuff[1024];
		while(1)
		{
			UINT nRead = pFile->Read((void *)szBuff, 1023);
			if(nRead > 0)
			{
				szBuff[nRead]='\0';
				strPage=strPage+szBuff;
			}
			else
				break;
		}

		pFile->Close();
		pServer->Close();
	}
	catch (CInternetException* pEx)
	{
		// catch errors from WinINet
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr, 1024);

		TRACE1("Error: (%d)\n", pEx->m_dwError);
	//	pEx->ReportError();

		nRetCode = 2;
		pEx->Delete();
	}
	catch (CWebException* pEx)
	{
		// catch things wrong with parameters, etc
		nRetCode = pEx->m_nErrorCode;
		TRACE1("Error: Exiting with CWebException(%d)\n", nRetCode);
		pEx->Delete();
	}

	if (pFile != NULL)
		delete pFile;
	if (pServer != NULL)
		delete pServer;
	session.Close();

	return nRetCode;
}

int CWebWorld::SendMessage(LPCTSTR pszURL,LPCTSTR pszPost)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	int nRetCode = 0;

	CInternetSession session(_T("FstInet"));
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	try
	{
		// check to see if this is a reasonable URL
		CString strServerName;
		CString strObject;
		INTERNET_PORT nPort;
		DWORD dwServiceType;
		CString strHead;

		if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
			dwServiceType != INTERNET_SERVICE_HTTP)
		{
			ThrowWebException(1);
		}

		pServer = session.GetHttpConnection(strServerName, nPort);

		pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,
			strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);

		pFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded\r\n"));
		pFile->SendRequest(szHeaders,_tcslen(szHeaders),(LPVOID)pszPost,_tcslen(pszPost));



		pFile->Close();
		pServer->Close();
	}
	catch (CInternetException* pEx)
	{
		// catch errors from WinINet
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr, 1024);

		TRACE1("Error: (%d)\n", pEx->m_dwError);
		//	pEx->ReportError();

		nRetCode = 2;
		pEx->Delete();
	}
	catch (CWebException* pEx)
	{
		// catch things wrong with parameters, etc
		nRetCode = pEx->m_nErrorCode;
		TRACE1("Error: Exiting with CWebException(%d)\n", nRetCode);
		pEx->Delete();
	}

	if (pFile != NULL)
		delete pFile;
	if (pServer != NULL)
		delete pServer;
	session.Close();

	return nRetCode;
}

int CWebWorld::SendMessage(LPCTSTR pszURL)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	int nRetCode = 0;

	CInternetSession session(_T("FstInet"));
	CHttpConnection* pServer = NULL;
	CHttpFile* pFile = NULL;
	try
	{
		// check to see if this is a reasonable URL
		CString strServerName;
		CString strObject;
		INTERNET_PORT nPort;
		DWORD dwServiceType;
		CString strHead;

		if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
			dwServiceType != INTERNET_SERVICE_HTTP)
		{
			ThrowWebException(1);
		}

		pServer = session.GetHttpConnection(strServerName, nPort);

		pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
			strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);

		pFile->SendRequest(szHeaders,_tcslen(szHeaders));


		pFile->Close();
		pServer->Close();
	}
	catch (CInternetException* pEx)
	{
		// catch errors from WinINet
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr, 1024);

		TRACE1("Error: (%d)\n", pEx->m_dwError);
		//	pEx->ReportError();

		nRetCode = 2;
		pEx->Delete();
	}
	catch (CWebException* pEx)
	{
		// catch things wrong with parameters, etc
		nRetCode = pEx->m_nErrorCode;
		TRACE1("Error: Exiting with CWebException(%d)\n", nRetCode);
		pEx->Delete();
	}

	if (pFile != NULL)
		delete pFile;
	if (pServer != NULL)
		delete pServer;
	session.Close();

	return nRetCode;
}

CString CWebWorld::getConfigXml()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	CString strGet;

	char szText[512]={0};
	char szPath[512]={0};

	int nSize = 0;

	if(GetCurrentDirectory(512,szPath) >100)
		return "";

	strcat(szPath,"\\cclib.ini");


	::GetPrivateProfileString("cclib","UpdateURL","http://www.cclib.com.cn/client/clientconfig.xml",szText,512,szPath);
	nSize = strlen(szText);
	if(nSize<5)
		return "";

	GetWebPage(szText,strGet);
	return strGet;
}

void CWebWorld::addAddressCookie(LPCTSTR szUrl)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	CStringArray arr;
	readMacaddress(arr);

	CString value;
	for(int i=0;i<arr.GetSize();i++)
	{
		value+=arr.GetAt(i)+" ";
	}
	value.Trim();

	InternetSetCookie(szUrl,"mac",value);
}